# qemu via uboot on arm64, for "virt" qemu machine type
declare -g BOARD_NAME="uefi-arm64"
declare -g BOARDFAMILY="uefi-arm64"
declare -g BOARD_MAINTAINER="rpardini"
declare -g KERNEL_TARGET="current,edge"

declare -g UEFI_GRUB="skip" # Skip GRUB for this board
declare -g SERIALCON="ttyAMA0"
declare -g QEMU_UBOOT_BOOTCONFIG="qemu_arm64_defconfig"

# qemu'ed uboot on arm64 can do extlinux.
# declare -g SRC_EXTLINUX="yes"
# declare -g SRC_CMDLINE="loglevel=7 console=ttyAMA0" # Those boards are serial-debug only; 'root=LABEL=armbi_root' ?
# enable_extension "u-boot-menu"                    # this generates the extlinux.conf

function post_family_config__force_uboot_bin_and_use_uboot_menu_extlinux_arm64() {
	declare -g UBOOT_TARGET_MAP="all;;u-boot.bin"

	# lets try with a bootscript. kernel does not start under extlinux...
	declare -g BOOTSCRIPT="boot-qemu-arm64.cmd:boot.cmd"
}

# Export the u-boot.rom file next to the disk image, so it can be used as qemu's firmware via the -bios option.
function pre_umount_final_image__export_uboot_rom() {
	: "${version:?version is not set}"
	local uboot_name="linux-u-boot-${BRANCH}-${BOARD}"
	display_alert "Exporting u-boot.bin" "${uboot_name} - ${version}.u-boot.bin" "debug"
	run_host_command_logged cp -pv "${MOUNT}/usr/lib/${uboot_name}/u-boot.bin" "${DESTIMG}/${version}.u-boot.bin"
}

function board_docs__qemu-uboot-arm64() {
	: "${version:?version is not set}"

	cat <<- DOCS
		## Running on qemu, via command line

		\`\`\`bash
		# nvme stuff
		qemu-system-aarch64 -m 2048 -machine virt -nographic -cpu cortex-a57 \\
			-bios ${version}.u-boot.bin \\
			-drive if=none,file=${version}.img.qcow2,id=armbian \\
			-device nvme,drive=armbian,serial=serialnumber

		# virtio, network
		# try with -accel kvm on supported neoverse systems
		qemu-system-aarch64 -m 2048 -machine virt -nographic -cpu cortex-a72 \\
			-bios ${version}.u-boot.bin -nic user,model=virtio-net-pci \\
			-drive if=none,media=disk,id=drive0,file=${version}.img.qcow2,discard=unmap,detect-zeroes=unmap \\
			-device virtio-blk-pci,drive=drive0,bootindex=0

		\`\`\`

		@TODO, see docs below.

		## Docs

		- https://u-boot.readthedocs.io/en/latest/board/emulation/qemu-arm.html
		- https://u-boot.readthedocs.io/en/latest/develop/devicetree/dt_qemu.html
	DOCS
}
